<?php

class warehouseController extends baseController
{
	public function show()
	{
		$this->assign('list',	Warehouse::getList());
		$this->display();
	}
	
	/**
	 * 增加和修改
	 */
	public function save()
	{
		$warehouse_id	= fetch_post('warehouse_id', 'int');

		if(!$warehouse_id && !fetch_post('name')) {
			Show::warning("请输入正确的名称");
		}

		if(!$warehouse_id)
		{
			$warehouse_id	= Warehouse::add(array(
					'name'		=> fetch_post('name'),
					'sno'		=> fetch_post('sno','char'),
					'user_id'	=> UID,
					'location'	=> fetch_post('location'),
					'remark'	=> fetch_post('remark')
				));
			Show::ajaxBand(array('method'=>'insert'));
		}
		else
		{
			Warehouse::update($warehouse_id, array(
					'name'		=> fetch_post('name'),
					'sno'		=> fetch_post('sno','char'),
					'user_id'	=> UID,
					'location'	=> fetch_post('location'),
					'remark'	=> fetch_post('remark')
				));
			Show::ajaxBand(array('method'=>'update'));
		}

		$this->assign('u', Warehouse::get($warehouse_id));
		$html = $this->fetch('warehouse-saved');

		Show::ajaxBand(array(
			'html'		=>	$html,
			'warehouse_id'	=>	$warehouse_id,
		));
		Show::notice("保存成功！");
	}

	/**
	  添加修改用户
	  */
	public function nature()
	{
		if($warehouse_id=fetch_get('warehouse_id', 'int')){
			if(!($warehouse=Warehouse::get($warehouse_id))){
				Show::ajaxLeave('fail', 'No such warehouse!');
			}
			else{
				$this->assign('warehouse', $warehouse);
			}
		}
		Show::ajaxLeave('ok', $this->fetch());
	}

	/**
	 * 删除
	 */
	public function delete()
	{
		if(!fetch_get('warehouse_id', 'int')) {
			Show::error("参数错误");
		}
		if(Warehouse::delete(fetch_get('warehouse_id', 'int'))) {
			Show::notice("删除成功");
		}
		else {
			Show::warning("删除失败");
		}
	}
	
}

?>
